#include "objectmodel.h"

POINT * ContourToPoints(const contour_t & contour, double scale, point_t const & position)
{
	POINT * ret = new POINT[contour.size()];
	for(int i = 0; i < contour.size(); i++ )
	{
		ret[i].x = (contour[i].x - position.x) * scale;
		ret[i].y = (contour[i].y - position.y) * scale;
	}
	return ret;
}

POINT PCON(const point_t & point, double scale, point_t const & position)
{
	POINT pt;
	pt.x = (point.x - position.x) * scale;
	pt.y = (point.y - position.y) * scale;
	return pt;	
}

point_t PTCON(const POINT & point, double scale, point_t const & position)
{
	point_t pt;
	pt.x = point.x / scale + position.x;
	pt.y = point.y / scale + position.y;
	return pt;
}

//object
object::object(polygon_t poly_, int id_) : poly(poly_), id(id_)
{
}
	
int object::getid() const
{
	return id;
}

void object::base_draw(HDC const * DC, double scale, point_t const & position) const
{
	SelectObject(*DC, pen);
	SelectObject(*DC, brush);

	POINT * point = ContourToPoints(poly[0], scale, position);
	Polygon(*DC, ContourToPoints(poly[0], scale, position), poly[0].size());
	delete[] point;

	for(int i = 1; i < poly.size(); i++)
	{
		HBRUSH BACK = CreateSolidBrush(GetSysColor(5));
		HPEN BPEN = CreatePen(PS_SOLID, 1, GetSysColor(5));
		SelectObject(*DC, BPEN);
		SelectObject(*DC, BACK);
		POINT * points = ContourToPoints(poly[i], scale, position);
		Polygon(*DC, points, poly[i].size());
		delete[] points;
	}
}

object * getobject(MAP_OBJECT * input)
{
	if (input->mtype == house)
		return new House(input->polygon,input->mobj_id);
	if (input->mtype == river)
		return new River(input->polygon,input->mobj_id);
	if (input->mtype == road)
		return new Road(input->polygon,input->mobj_id);
	if (input->mtype == green)
		return new Green(input->polygon,input->mobj_id);
	return 0;
}


//Object's inheritors

House::House(polygon_t poly_, int id_) : object(poly_, id_)
{
	brush = CreateSolidBrush(RGB(20, 20, 20));
	pen = CreatePen(0, 1, RGB(0, 0, 0));
}

void House::draw(HDC const * DC, double scale, point_t const & position) const 
{
	base_draw(DC, scale, position);
}

River::River(polygon_t poly_, int id_) : object(poly_, id_)
{
	brush = CreateSolidBrush(RGB(0, 0, 120));
	pen = CreatePen(0, 1, RGB(0, 0, 120));
}

void River::draw(HDC const * DC, double scale, point_t const & position) const
{
	SelectObject(*DC, pen);
	SelectObject(*DC, brush);

	POINT * p_arr = ContourToPoints(poly[0], scale, position);
	MoveToEx(*DC, p_arr[0].x, p_arr[0].y, (LPPOINT) NULL);
	for(size_t j = 1; j < poly[0].size() / 2; ++j)
	{
		LineTo(*DC, p_arr[j].x, p_arr[j].y);
	}
	delete[] p_arr;
}


Road::Road(polygon_t poly_,int id_) : object(poly_, id_)
{
	brush = CreateSolidBrush(RGB(100, 150, 100));
	pen = CreatePen(0, 1, RGB(20, 20, 20));
}

Green::Green(polygon_t poly_, int id_) : object(poly_, id_)
{
	brush = CreateSolidBrush(RGB(20, 120, 20));
	pen = CreatePen(0, 1, RGB(0, 100, 0));
}

void Green::draw(HDC const * DC, double scale, point_t const & position) const 
{
	base_draw(DC, scale, position);
}


void Road::draw(HDC const * DC, double scale, point_t const & position) const
{
	SelectObject(*DC, pen);
	SelectObject(*DC, brush);

	POINT * p_arr = ContourToPoints(poly[0], scale, position);
	MoveToEx(*DC, p_arr[0].x, p_arr[0].y, (LPPOINT) NULL);
	for(size_t j = 1; j < poly[0].size() / 2; ++j)
	{
		LineTo(*DC, p_arr[j].x, p_arr[j].y);
	}
	delete[] p_arr;
}
